﻿using System.Text;
using System.Threading.Tasks;
using Admin.Common;
using Admin.Common.Tool;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Admin.Web.Controllers.Api
{
    /// <summary>
    /// 公用操作
    /// </summary>
    public class CommonController : ApiController
    {
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task CaptchaImage()
        {
            string code = "";
            //生成验证码
            byte[] bt = VerifyCodeHelper.GetVerifyCode(out code);
            //Session中存储验证码
            HttpContext.Session.SetString("LoginCaptcha", code);

            await DownloadAsync(bt, "Captcha", Encoding.UTF8);
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filePath">文件绝对路径</param>
        /// <param name="fileName">文件名,包含扩展名</param>
        [HttpGet]
        public async Task Download(string filePath)
        {
            string Path = Appsettings.app(new string[] { "AppSettings", "File", "Download", "Excel" });
            var bytes = System.IO.File.ReadAllBytes($"{Path}{filePath}");
            await DownloadAsync(bytes, filePath, Encoding.UTF8);
            System.IO.File.Delete($"{Path}{filePath}");
        }
    }
}